package com.itheima.exception;

import com.itheima.common.R;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice
public class MyExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R handlerSqlException(SQLIntegrityConstraintViolationException e){

        e.printStackTrace();
        String message = e.getMessage();


        if (message.contains("Duplicate entry")){
            String name = message.split(" ")[2];

            return R.error(name+"已存在");

        }
        return R.error("未知错误");
    }

    @ExceptionHandler(CustomException.class)
    public R handlerCustomException(CustomException e){
        e.printStackTrace();
       return R.error(e.getMessage()) ;

    }


}
